//app.js
var utils = require('./utils/utils')
var httpUtils = require('./utils/httpUtils')
var commonFields = require('./utils/commonFields')
var Promise = require('./utils/promise')
App({

    onLaunch: function (options) {
        console.log("app onLoaunch options", options)
        // Do something when show.
        console.log("app onShow options", options)
        httpUtils.postShareQR()
        let that = this;
        // that.getUserInfo().then((res) => {
        //     console.log("app getUserInfo 正常回调", res)
        // }).catch((err) => {
        //     console.log("app getUserInfo 错误回调", err)
        // });
        that.getLogin().then((res) => {
            console.log("login ", res)
            return {
                code: res.code,
                test: "yoyo",
            }
        }).then((obj) => {
            console.log("上面那个then传过来的数据", obj)
            that.getOpenId(obj.code).then(res => {
                console.log("getOpenId ", res)
            }).catch(err => {
                console.log("getOpenId err", err)
            })
        }).then(() => {
            wx.getStorageInfo({
                success: res => {
                    console.log("getStorageInfo", res)
                }
            })
        }).then(() => {
            let systemInfo = wx.getSystemInfoSync();
            console.log("systemInfo", systemInfo)
            if (systemInfo.model.toLowerCase().indexOf('iphone') > -1 || systemInfo.system.toLowerCase().indexOf('ios') > -1) {
                console.log("model", systemInfo.model.toLowerCase())
                console.log("ios", systemInfo.system.toLowerCase())
                utils.setStorageSync(commonFields.isIOS, true)
            } else {
                utils.setStorageSync(commonFields.isIOS, false)
            }
            return systemInfo
        }).then(() => {
            let userInfo = utils.getStorageSync(commonFields.userInfo);
        })
    },
    onShow(options) {

    },
    onHide() {
        // Do something when hide.
    },
    onError(msg) {
        console.log(msg)
        console.log("app onError msg", msg)
    },

    getLogin() {
        return new Promise(function (resolve, reject) {
            wx.login({
                success: res => {
                    resolve(res)
                },
                fail: error => {
                    console.log("app login error")
                    reject(error)
                }
            })
        });
    },
    getOpenId(code) {
        return new Promise(function (resolve, reject) {
            httpUtils.postOpenId(code, res => {
                if (res.code == 0) {
                    let obj = JSON.parse(res.data)
                    utils.setStorageSync(commonFields.openid, obj.openid)
                    resolve(obj)
                } else {
                    reject(JSON.parse(res))
                }
            },
                err => {
                    console.log("app getOpenId err", err)
                    reject(err)
                })
        });
    },

    getUserInfo() {
        return new Promise(function (resolve, reject) {
            wx.getSetting({
                success(res) {
                    let isScopeUserInfo = res.authSetting['scope.userInfo'];
                    console.log("app getUserInfo isScopeUserInfo", isScopeUserInfo)
                    if (!isScopeUserInfo) {
                        wx.authorize({
                            scope: 'scope.userInfo',
                            success() {
                                // 用户已经同意小程序使用录音功能，后续调用 wx.getUserInfo 接口不会弹窗询问
                                // 必须是在用户已经授权的情况下调用
                                wx.getUserProfile({
                                    desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
                                    success(res) {
                                        utils.setStorageSync(commonFields.wxUserInfo, res.userInfo)
                                        resolve(res)
                                    }
                                })
                            },
                            fail(err) {
                                console.log("app getUserInfo is error", err)
                                reject(err)
                            }
                        })
                    } else {
                        console.log("app auth userInfo 已经有权限了")
                    }
                }
            })
        })
    },

})
